/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026;

import android.content.Context;
import android.text.TextUtils;

import java.util.Map;
import java.util.Set;

/**
 * 应用存储
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public final class JKJSharedPreference extends SharedPreferenceUtil {

    private static JKJSharedPreference mInstance;

    public static void init(Context context, String name) {
        if (mInstance == null) {
            mInstance = new JKJSharedPreference(context, name);
        }
    }

    public static JKJSharedPreference getInstance() {
        return mInstance;
    }

    private JKJSharedPreference(Context context, String name) {
        super(context, name);
    }

    /**
     * 点击更新过的版本
     */
    public void putUpdateVersion(int code) {
        put("jkj_update_code", code);
    }

    /**
     * 设置更新过的版本
     */
    public int getUpdateVersion() {
        return getInt("jkj_update_code", 0);
    }

    /**
     * 设置不弹出更新
     */
    public void putShowUpdate(boolean isShow) {
        put("jkj_update_show", isShow);
    }

    /**
     * 是否弹出更新
     * 或者是新版本重新更新 259200000
     */
    boolean isShowUpdate() {
        return getBoolean("jkj_update_show", true);
    }

    /**
     * 是否已经弹出更新
     *
     * @return 不弹出更新代表已经更新
     */
    public boolean hasShowUpdate() {
        return !getBoolean("jkj_update_show", true);
    }

    /**
     * 设备唯一标示
     *
     * @param id id
     */
    public void putDeviceUUID(String id) {
        put("jkj_device_uuid", id);
    }

    /**
     * 设备唯一标示
     */
    public String getDeviceUUID() {
        return getString("jkj_device_uuid", "");
    }


    /**
     * 第一次安装
     */
    public void putFirstInstall() {
        put("jkj_first_install", false);
    }

    /**
     * 第一次安装
     */
    public boolean isFirstInstall() {
        return getBoolean("jkj_first_install", true);
    }

    /**
     * 第一次使用
     */
    public void putFirstUsing() {
        put("jkj_first_using_v2", false);
    }

    /**
     * 第一次使用
     */
    public boolean isFirstUsing() {
        return getBoolean("jkj_first_using_v2", true);
    }


    /**
     * putLastNewsId
     *
     * @param id id
     */
    public void putLastNewsId(long id) {
        put("last_news_id", id);
    }

    /**
     * 获取最新的id
     *
     * @return return
     */
    public long getLastNewsId() {
        return getLong("last_news_id", 0);
    }


    /**
     * 返回新闻有多少
     */
    public long getTheNewsId() {
        return getLong("the_last_news_id", 0);
    }

    /**
     * 存储最新新闻数量
     */
    public void putTheNewsId(long count) {
        put("the_last_news_id", count);
    }


    /**
     * 关联剪切版
     *
     * @param isRelate isRelate
     */
    public void putRelateClip(boolean isRelate) {
        put("jkj_is_relate_clip", isRelate);
    }

    /**
     * 是否关联剪切版
     *
     * @return 是否关联剪切版
     */
    public boolean isRelateClip() {
        return getBoolean("jkj_is_relate_clip", true);
    }

    /**
     * 最后一次分享的url
     *
     * @param url 最后一次分享的url
     */
    public void putLastShareUrl(String url) {
        if (TextUtils.isEmpty(url))
            return;
        put("jkj_last_share_url", url);
    }

    /**
     * 最后一次分享的url
     *
     * @return 最后一次分享的url
     */
    public String getLastShareUrl() {
        return getString("jkj_last_share_url", "");
    }


    public boolean isFirstOpenUrl() {
        return getBoolean("jkj_is_first_open_url", true);
    }

    public void putFirstOpenUrl() {
        put("jkj_is_first_open_url", false);
    }

    public static <T> T getValue(String key, T defaultValue) {
        return getAll().get(key) == null ? defaultValue : (T) getAll().get(key);
    }

    public static Map<String, ?> getAll() {
        return getInstance().mSp.getAll();
    }


    public static JKJSharedPreference save(String key, String value) {
        getInstance().mEditor.putString(key, value).commit();
        return getInstance();
    }

    public static JKJSharedPreference save(String key, boolean value) {
        getInstance().mEditor.putBoolean(key, value).commit();
        return getInstance();
    }

    public static JKJSharedPreference save(String key, float value) {
        getInstance().mEditor.putFloat(key, value).commit();
        return getInstance();
    }

    public static JKJSharedPreference save(String key, long value) {
        getInstance().mEditor.putLong(key, value).commit();
        return getInstance();
    }

    public static JKJSharedPreference save(String key, int value) {
        getInstance().mEditor.putInt(key, value).commit();
        return getInstance();
    }

    public static JKJSharedPreference save(String key, Set<String> value) {
        getInstance().mEditor.putStringSet(key, value).commit();
        return getInstance();
    }
}
